package com.lzk.validator;

import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.upload.UploadFile;
import com.jfinal.validate.Validator;
import com.lzk.service.UserService;
import com.lzk.service.impl.UserServiceImpl;

public class UserValidator extends Validator {

    UserService us = new UserServiceImpl();

    @Override
    protected void validate(Controller c) {
        setRet(Ret.fail());
        if ("/back/user/add".equals(getActionKey())) {
            c.getFile();
            validateRequired("userName", "msg", "用户名不得为空");
            validateRequired("pwd", "msg", "密码不得为空");
            validateRequired("userStatus", "msg", "用户状态不得为空");
            //用户名重复性验证
            String userName = c.getPara("userName");
            if (userName != null) {
                boolean flag = us.checkUserName(userName);
                if (flag) {
                    addError("msg", "用户名重复！");
                }
            }
        }
    }

    @Override
    protected void handleError(Controller c) {
        c.renderJson(getRet());
    }

}
